//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "s:\packages\eic\eic.h"
#include "CEICDatabase.h"
#pragma package(smart_init)
//---------------------------------------------------------------------------
// ValidCtrCheck est utilisé est utilisé pour être sûr que les composants créés n'ont
// aucune fonction virtuelle pure.
//

static inline void ValidCtrCheck(CEICDatabase *)
{
  new CEICDatabase(NULL);
}
//---------------------------------------------------------------------------
__fastcall CEICDatabase::CEICDatabase(TComponent* Owner) : TComponent(Owner)
{
  FBDEstInterbase = false;
  FParams_Tmp     = new TStringList();
}
//---------------------------------------------------------------------------
__fastcall CEICDatabase::~CEICDatabase()
{
  if( FDatabase && (FAliasName_Tmp != EmptyStr) && Session->IsAlias(FAliasName_Tmp)) Session->DeleteAlias(FAliasName_Tmp);
  if( FIBDatabase) {delete FIBDatabase; FIBDatabase = NULL;}
  if( FDatabase)   {delete FDatabase;   FDatabase   = NULL;}
  if( FParams_Tmp) {delete FParams_Tmp; FParams_Tmp = NULL;}
}

/********************************
 *          PROPRIETES          *
 ********************************/
//---------------------------------------------------------------------------
// Propriété "AliasName"
//---------------------------------------------------------------------------
AnsiString __fastcall CEICDatabase::GetAliasName(void)
{
  if(ComponentState.Contains( csReading) && Connected) return FAliasName_Tmp;

  if( BDEstInterbase && FIBDatabase) return EmptyStr;
  if( FDatabase)                     return FDatabase->AliasName;
  else                               return FAliasName_Tmp;
}
void __fastcall CEICDatabase::SetAliasName(const AnsiString Value)
{
  FAliasName_Tmp = Value;
  if( BDEstInterbase && FIBDatabase) FAliasName_Tmp = EmptyStr;
  if( FDatabase)
  {
    if( ComponentState.Contains( csReading) && Connected)
    {
      Connected = false;
      FDatabase->AliasName = FAliasName_Tmp;
      Connected = true;
    }
    else FDatabase->AliasName = FAliasName_Tmp;
  }
}
//---------------------------------------------------------------------------
// Propriété "Connected"
//---------------------------------------------------------------------------
bool __fastcall CEICDatabase::GetConnected(void)
{
  if( BDEstInterbase && FIBDatabase) return FIBDatabase->Connected;
  else if( FDatabase)                return FDatabase->Connected;
  else                               return false;
}
void __fastcall CEICDatabase::SetConnected(bool Value)
{
  if( BDEstInterbase)
  {
    if( !FIBDatabase)
    {
      FIBDatabase = new TIBDatabase(NULL);
      //Affectation des Propriétés Published
      FIBDatabase->DatabaseName = FDatabaseName_Tmp;
      FIBDatabase->LoginPrompt  = FLoginPrompt_Tmp;
      FIBDatabase->Params->Assign( FParams_Tmp);
      //Affectation des Evènements
      FIBDatabase->AfterConnect     = FAfterConnect_Tmp;
      FIBDatabase->AfterDisconnect  = FAfterDisconnect_Tmp;
      FIBDatabase->BeforeConnect    = FBeforeConnect_Tmp;
      FIBDatabase->BeforeDisconnect = FBeforeDisconnect_Tmp;
      FIBDatabase->OnLogin          = IBDbLoginEvent;
    }
    if( FIBDatabase) FIBDatabase->Connected = Value;
  }
  else
  {
    if( !FDatabase)
    {
      FDatabase = new TDatabase(NULL);
      //Affectation des Propriétés Published
      FDatabase->AliasName    = FAliasName_Tmp;
      if( !Session->IsAlias(FAliasName_Tmp))
        Session->AddStandardAlias( FAliasName_Tmp, FDatabaseName_Tmp, "PARADOX");
      FDatabase->DatabaseName = FAliasName_Tmp + "_db";
      FDatabase->LoginPrompt  = FLoginPrompt_Tmp;
      FDatabase->Params->Assign( FParams_Tmp);
      //Affectation des Evènements
      FDatabase->AfterConnect     = FAfterConnect_Tmp;
      FDatabase->AfterDisconnect  = FAfterDisconnect_Tmp;
      FDatabase->BeforeConnect    = FBeforeConnect_Tmp;
      FDatabase->BeforeDisconnect = FBeforeDisconnect_Tmp;
      FDatabase->OnLogin          = DbLoginEvent;
    }
    if( FDatabase) FDatabase->Connected = Value;
  }
}
//---------------------------------------------------------------------------
// Propriété "DatabaseName"
//---------------------------------------------------------------------------
AnsiString __fastcall CEICDatabase::GetDatabaseName(void)
{
  return FDatabaseName_Tmp;
}
void __fastcall CEICDatabase::SetDatabaseName(const AnsiString Value)
{
  FDatabaseName_Tmp = Value;
  if( BDEstInterbase && FIBDatabase) FIBDatabase->DatabaseName = Value;
  else if( FDatabase)                SetConnected(FDatabase->Connected);
}
//---------------------------------------------------------------------------
// Propriété "LoginPrompt"
//---------------------------------------------------------------------------
bool __fastcall CEICDatabase::GetLoginPrompt(void)
{
  if(ComponentState.Contains( csReading) && Connected) return FLoginPrompt_Tmp;

  if( BDEstInterbase && FIBDatabase) return FIBDatabase->LoginPrompt;
  else if( FDatabase)                return FDatabase->LoginPrompt;
  else                               return FLoginPrompt_Tmp;
}
void __fastcall CEICDatabase::SetLoginPrompt(bool Value)
{
  FLoginPrompt_Tmp = Value;
  if( BDEstInterbase && FIBDatabase) FIBDatabase->LoginPrompt = Value;
  else if( FDatabase)                FDatabase->LoginPrompt   = Value;
}
//---------------------------------------------------------------------------
// Propriété "Params"
//---------------------------------------------------------------------------
Classes::TStrings* __fastcall CEICDatabase::GetDBParams(void)
{
  if(ComponentState.Contains( csReading) && Connected) return FParams_Tmp;

  if( BDEstInterbase && FIBDatabase) return FIBDatabase->Params;
  else if( FDatabase)                return FDatabase->Params;
  else                               return FParams_Tmp;
}
void __fastcall CEICDatabase::SetDBParams(Classes::TStrings* Value)
{
  FParams_Tmp->Assign( Value);
  if( BDEstInterbase && FIBDatabase) FIBDatabase->Params->Assign( Value);
  else if( FDatabase)                FDatabase->Params->Assign( Value);
}
//---------------------------------------------------------------------------
// Propriété "SessionName"
//---------------------------------------------------------------------------
AnsiString __fastcall CEICDatabase::GetSessionName(void)
{
  if(ComponentState.Contains( csReading) && Connected) return FSessionName_Tmp;

  if( BDEstInterbase && FIBDatabase) return EmptyStr;
  else if( FDatabase)                return FDatabase->SessionName ;
  else                               return FSessionName_Tmp ;
}
void __fastcall CEICDatabase::SetSessionName(const AnsiString Value)
{
  FSessionName_Tmp = Value;
  if( !BDEstInterbase && FDatabase)
  {
    if(ComponentState.Contains( csReading) && Connected)
    {
      Connected = false; FDatabase->SessionName = Value; Connected = true;
    }
    else FDatabase->SessionName = Value;
  }
}

/******************************
 *          METHODES          *
 ******************************/
void __fastcall CEICDatabase::Close(void)
{
  if( BDEstInterbase && FIBDatabase) FIBDatabase->Close();
  else if(FDatabase)                 FDatabase->Close();
}
void __fastcall CEICDatabase::CloseDataSets(void)
{
  if( BDEstInterbase && FIBDatabase) FIBDatabase->CloseDataSets();
  else if(FDatabase)                 FDatabase->CloseDataSets();
}
void __fastcall CEICDatabase::Open(void)
{
  if( BDEstInterbase && FIBDatabase) FIBDatabase->Open();
  else if( FDatabase)                FDatabase->Open();
}

/********************************
 *          EVENEMENTS          *
 ********************************/
//---------------------------------------------------------------------------
// Evènement "AfterConnect"
//---------------------------------------------------------------------------
TNotifyEvent __fastcall CEICDatabase::GetAfterConnect(void)
{
  if( BDEstInterbase && FIBDatabase) return FIBDatabase->AfterConnect;
  else if( FDatabase)                return FDatabase->AfterConnect;
  else                               return FAfterConnect_Tmp;
}
void __fastcall CEICDatabase::SetAfterConnect(const TNotifyEvent Value)
{
  FAfterConnect_Tmp = Value;
  if( BDEstInterbase && FIBDatabase) FIBDatabase->AfterConnect = Value;
  else if( FDatabase)                FDatabase->AfterConnect   = Value;
}
//---------------------------------------------------------------------------
// Evènement "AfterDisconnect"
//---------------------------------------------------------------------------
TNotifyEvent __fastcall CEICDatabase::GetAfterDisconnect(void)
{
  if( BDEstInterbase && FIBDatabase) return FIBDatabase->AfterDisconnect;
  else if( FDatabase)                return FDatabase->AfterDisconnect;
  else                               return FAfterDisconnect_Tmp;
}
void __fastcall CEICDatabase::SetAfterDisconnect(const TNotifyEvent Value)
{
  FAfterDisconnect_Tmp = Value;
  if( BDEstInterbase && FIBDatabase) FIBDatabase->AfterDisconnect = Value;
  else if( FDatabase)                FDatabase->AfterDisconnect   = Value;
}
//---------------------------------------------------------------------------
// Evènement "BeforeConnect"
//---------------------------------------------------------------------------
TNotifyEvent __fastcall CEICDatabase::GetBeforeConnect(void)
{
  if( BDEstInterbase && FIBDatabase) return FIBDatabase->BeforeConnect;
  else if( FDatabase)                return FDatabase->BeforeConnect;
  else                               return FBeforeConnect_Tmp;
}
void __fastcall CEICDatabase::SetBeforeConnect(const TNotifyEvent Value)
{
  FBeforeConnect_Tmp = Value;
  if( BDEstInterbase && FIBDatabase) FIBDatabase->BeforeConnect = Value;
  else if( FDatabase)                FDatabase->BeforeConnect   = Value;
}
//---------------------------------------------------------------------------
// Evènement "BeforeDisconnect"
//---------------------------------------------------------------------------
TNotifyEvent __fastcall CEICDatabase::GetBeforeDisconnect(void)
{
  if( BDEstInterbase && FIBDatabase) return FIBDatabase->BeforeDisconnect;
  else if( FDatabase)                return FDatabase->BeforeDisconnect;
  else                               return FBeforeDisconnect_Tmp;
}
void __fastcall CEICDatabase::SetBeforeDisconnect(const TNotifyEvent Value)
{
  FBeforeDisconnect_Tmp = Value;
  if( BDEstInterbase && FIBDatabase) FIBDatabase->BeforeDisconnect = Value;
  else if( FDatabase)                FDatabase->BeforeDisconnect   = Value;
}
//---------------------------------------------------------------------------
// Evènement "OnLogin"
//---------------------------------------------------------------------------
void __fastcall CEICDatabase::DbLoginEvent(TDatabase* Database, Classes::TStrings* LoginParams)
{
  if( FOnLogin)
  {
    ASSERT( Database == FDatabase);
    FOnLogin( this,LoginParams);
  }
}
void __fastcall CEICDatabase::IBDbLoginEvent(TIBDatabase* Database, Classes::TStrings* LoginParams)
{
  if( FOnLogin)
  {
    ASSERT( Database == FIBDatabase);
    FOnLogin( this,LoginParams);
  }
}



//---------------------------------------------------------------------------
namespace Ceicdatabase
{
  void __fastcall PACKAGE Register()
  {
     TComponentClass classes[1] = {__classid(CEICDatabase)};
     RegisterComponents("EIC Query", classes, 0);
  }
}
//---------------------------------------------------------------------------
 